package summary;

import java.util.LinkedList;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/13 15:13
 * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
 */
public class Title111 {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int minDepth(TreeNode root) {
        int maxDepth = 0;
        if (root == null) {
            return maxDepth;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.offer(root);
        while (!list.isEmpty()) {
            int size = list.size();
            maxDepth++;
            for (int i = 0; i < size; i++) {
                TreeNode pop = list.pop();
                if (pop.left == null && pop.right == null) {
                    return maxDepth;
                }
                if (pop.left != null) {
                    list.offer(pop.left);
                }
                if (pop.right != null) {
                    list.offer(pop.right);
                }
            }

        }
        return maxDepth;
    }

    public int minDepth1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int minDepth = Integer.MAX_VALUE;
        if (root.left != null) {
            minDepth = Math.min(minDepth, minDepth1(root.left));
        }
        if (root.right != null) {
            minDepth = Math.min(minDepth, minDepth1(root.right));
        }
        return minDepth + 1;
    }



    public int minDepth3(TreeNode root) {
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.offer(root);
        int height = 0;
        while (!list.isEmpty()) {
            int size = list.size();
            for (int i = 0; i < size; i++) {
                height++;
                TreeNode poll = list.poll();
                if (poll.left == null && poll.right == null) {
                    return height;
                }
                if (poll.left != null) {
                    list.offer(poll.left);
                }
                if (poll.right != null) {
                    list.offer(poll.right);
                }
            }
        }
        return -1;
    }


    public int minDepth4(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int minDepth = Integer.MAX_VALUE;
        if (root.left != null) {
            minDepth = Math.min(minDepth, minDepth4(root.left));
        }
        if (root.right != null) {
            minDepth = Math.min(minDepth, minDepth4(root.right));
        }
        return minDepth + 1;
    }


    public static void main(String[] args) {

    }
}
